package com.chara.rabbitmq_demo.com.chara.rabbitmq.topic;

import com.chara.rabbitmq_demo.com.chara.rabbitmq.utils.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @Classname Producer
 * @Description 主题模式生产者
 * @Date 2025/10/11 08:00
 * @Author Chara
 */
public class Producer {

    public static void main(String[] args) throws Exception {

        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        String exchangeName = "ex_topic";

        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,true,false,false,null);

        String queue1Name = "topic_queue1";
        String queue2Name = "topic_queue2";

        channel.queueDeclare(queue1Name,true,false,false,null);
        channel.queueDeclare(queue2Name,true,false,false,null);

        // 绑定队列与交换机
        //参数1： 队列名称
        //参数2： 交换机名称
        //参数3： 路由key
        // routingKey常用格式：系统的名称，日志级别
        // 需求：所有error级别的日志存入数据库，所有order系统日志存入数据库
        channel.queueBind(queue1Name,exchangeName,"#.error");
        channel.queueBind(queue1Name,exchangeName,"order.*");
        channel.queueBind(queue2Name,exchangeName,"*.*");

        // 分别发送消息队列：order.info,goods.info,goods.error
        String body = "[所在系统：order][日志级别：info][日志内容：订单生成保存成功]";
        channel.basicPublish(exchangeName,"order.info",null,body.getBytes());
        channel.basicPublish(exchangeName,"goods.info",null,body.getBytes());
        channel.basicPublish(exchangeName,"goods.error",null,body.getBytes());

        channel.close();
        connection.close();


    }
}
